package ma.dev.sowondejong.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.Request;
import com.facebook.Request.GraphUserCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import ma.dev.sowondejong.R;
import ma.dev.sowondejong.util.SharedPreferenceUtil;

public class MainFragment extends Fragment {
	
	private static final String TAG = "MainFragment";
	
	private UiLifecycleHelper uiHelper;
    @NotNull
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(final Session session, @NotNull final SessionState state, final Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
    
	@Nullable
    @Override
	public View onCreateView(@NotNull LayoutInflater inflater,
			ViewGroup container, 
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fb_login, container, false);

		LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
		authButton.setFragment(this);
		//authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
        
		return view;
	}
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        
        // For scenarios where the main activity is launched and user
		// session is not null, the session state change notification
		// may not be triggered. Trigger it if it's open/closed.
		Session session = Session.getActiveSession();
		if (session != null &&
				(session.isOpened() || session.isClosed()) ) {
			onSessionStateChange(session, session.getState(), null);
		}
		
        uiHelper.onResume();
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
    
    private void onSessionStateChange(Session session, @NotNull SessionState state, Exception exception) {
    	if (state.isOpened()) {
    		Request.executeMeRequestAsync(session, new GraphUserCallback() {
				
				@Override
				public void onCompleted(@NotNull GraphUser user, Response response) {
					SharedPreferenceUtil.putSharedPreference("FB_NAME", user.getName());
				}
			});
    		Log.i(TAG, "Logged in...");
        } else if (state.isClosed()) {
        	Log.i(TAG, "Logged out...");
        }
    }
}
